<style>
    /*.tncode{*/
    /*    background-color: #07bb8a;*/
    /*    color: white;*/
    /*    font-size: 20px;*/
    /*    height: 55px;*/
    /*    line-height: 55px;*/
    /*    border: none;*/
    /*    border-radius: 10px;*/
    /*}*/
</style>

<p style="font-size: 15px;display: table-cell; vertical-align:middle;color: #cccccc; padding-bottom: 10px">
    <span>手机端已上线，建议使用QQ浏览器进行观看舒适度更佳！【体验卡和周卡无下载视频功能】</span>
</p>
<div class="input-group input-group-lg parse-form">
    <input type="text" class="form-control parse-link" placeholder="请输入需要播放的视频地址">
    <input type="text" id="raw_link" style="display: none">

    <div class="input-group-append">
        <button class="btn-info input-group-text parse-btn" {if !$_G['uid']}disabled{/if}>解析</button>
<!--        <div class="tncode">解析</div>-->
        <div class="tncode" style="display: none"></div>
    </div>
</div>
<div class="card mt-3 d-none parse-result">
    <div class="card-header">资源解析结果</div>
    <div class="card-body">资源获取中，请稍后...</div>
    <div class="card-footer d-none"></div>
</div>
<div class="row mt-3">
    <div class="col-9">
        {if !$_G['uid']}
        <div class="alert alert-info">您需要先登录后才可使用解析视频的功能！！</div>
        {else}
        {if $_G['user']['vip_type'] == -2}
        <div class="alert alert-danger">欢迎您，{$_G['user']['username']}。您当前暂无解析权限，请购买卡密进行充值
            {if $_G['user']['free_times'] == 1} <span class="alert-warning">           新用户可免费体验一次</span>{/if}
            </div>

        {else}
        <div class="alert alert-success">欢迎您，{$_G['user']['username']}。您当前是：{$vip_type}. 过期时间: {$_G['user']['vip_expire_time']}
                {if $is_expire == 1} <span class="alert-danger">当前已过期</span>{/if}
        </div>
        {/if}
        {/if}
        <div class="row m-0 border border-left-0 bg-white" style="margin-left: 20px;">
            <div id="dplayer" style="width: 840px; height: 400px; background-color: #0C0C0C"></div>
        </div>
    </div>
    <div class="col-3 pl-0" style="filter:alpha(Opacity=80);-moz-opacity:0.8;opacity: 0.8;">
        <div class="index-user-bar bg-black border border-bottom-0">
            <div class="d-flex justify-content-start align-items-center">
                {volist name="user_nav" id="nav" key="n_i"}
                    <a class="w-50 text-center p-3 {if $n_i % 2 != 0}border-right{/if}" style="color: #fff" target="_blank"  href="{$nav['url']}">
                    <div>
                        <svg class="iconfont" aria-hidden="true">
                            <use xlink:href="#{$nav['icon']}"></use>
                        </svg>
                    </div>
                    <div>{$nav['name']}</div>
                </a>
                {if $n_i % 2 == 0}
                    </div>
                    <div class="d-flex justify-content-start align-items-center border-top">
                {/if}
                {/volist}

                    </div>
            <div class="d-flex justify-content-start align-items-center border-top">
                <a class="w-50 text-center p-3 border-right" style="color: yellow;cursor:pointer" id="download-source">
                    <div>
                        <svg class="iconfont" aria-hidden="true">
                            <use xlink:href="#icon-message"></use>
                        </svg>
                    </div>
                    <div>源文件</div>
                </a>
                <a class="w-50 text-center p-3" style="color: yellow;cursor:pointer" id="download-media">
                    <div>
                        <svg class="iconfont" aria-hidden="true">
                            <use xlink:href="#icon-message"></use>
                        </svg>
                    </div>
                    <div>本课素材</div>
                </a>
            </div>
            {if $_G['user']['vip_type'] != 0 && $_G['user']['vip_type'] != 1}
            <div class="d-flex justify-content-start align-items-center border-top">
                <a class="w-50 text-center p-3 Clipboard" id="copy_link" style="color: yellow;cursor:pointer;" data-clipboard-text="">
                    <div>
                        <svg class="iconfont" aria-hidden="true">
                            <use xlink:href="#icon-download"></use>
                        </svg>
                    </div>
                    <div >复制下载地址</div>
                </a>
                <a class="w-50 text-center p-3 border-right" href="https://www.bilibili.com/video/av46354541?from=search&seid=12798392543382347507" style="color: yellow;cursor:pointer;" target="_blank">
                    <div>
                        <svg class="iconfont" aria-hidden="true">
                            <use xlink:href="#icon-show-info"></use>
                        </svg>
                    </div>
                    <div >查看下载教程</div>
                </a>
            </div>
            {/if}
            <div class="d-flex justify-content-start align-items-center border-top"></div>
        </div>
    </div>
</div>

<link rel="stylesheet" href="static/css/DPlayer.min.css">
<link rel="stylesheet" href="static/tncode/tn_code.css">
<script src="static/js/hls.min.js" type="text/javascript" charset="utf-8"></script>
<script src="static/js/DPlayer.min.js" type="text/javascript" charset="utf-8"></script>
<script src="static/tncode/tn_code.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
    // const dp = new DPlayer({
    //     container: document.getElementById('dplayer'),
    //     autoplay: true,
    //     loop: true,
    //     screenshot: true,
    //     video: {
    //         url: 'http://image.openows.com/yongqi.mp4',
    //         type: 'auto',
    //     },
    // });

    if (IEVersion() !== -1){
        alert('不支持IE浏览器哦，若您使用的是360浏览器请切换为极速模式或直接更换为360极速浏览器')
    }

    function IEVersion() {
        var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
        var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1; //判断是否IE<11浏览器
        var isEdge = userAgent.indexOf("Edge") > -1 && !isIE; //判断是否IE的Edge浏览器
        var isIE11 = userAgent.indexOf('Trident') > -1 && userAgent.indexOf("rv:11.0") > -1;
        if(isIE) {
            var reIE = new RegExp("MSIE (\\d+\\.\\d+);");
            reIE.test(userAgent);
            var fIEVersion = parseFloat(RegExp["$1"]);
            if(fIEVersion == 7) {
                return 7;
            } else if(fIEVersion == 8) {
                return 8;
            } else if(fIEVersion == 9) {
                return 9;
            } else if(fIEVersion == 10) {
                return 10;
            } else {
                return 6;//IE版本<=7
            }
        } else if(isEdge) {
            return 'edge';//edge
        } else if(isIE11) {
            return 11; //IE11
        }else{
            return -1;//不是ie浏览器
        }
    }


    var link = '';
    var source_url = '';
    var media_url = '';
    var m3u8_url = '';
    var videoParse = false;

    tncode.init("验证");

    $(".parse-btn").click(function() {

        if (IEVersion() !== -1){
            alert('不支持IE浏览器哦，若您使用的是360浏览器请切换为极速模式或直接更换为360极速浏览器')
        }

        $(".tncode").click()

        tncode.onsuccess(function(){
            if ($('.parse-btn').hasClass('disabled')) {
                return false;
            }

            link = $.trim($('.parse-link').val());

            if (!link) {
                $('.parse-link').focus();
                return dialog.msg('请输入需要解析的虎课网课程网址');
            }

            var n = link.search(/huke88/i);
            if (n == "-1") {
                return dialog.msg('请输入正确的虎课网课程地址，可查看视频教程演示操作步骤');
            }

            var str = link;
            var match = str.match(/\/(\d.*)\.html/)
            var hukeID = match[1];

            if (hukeID.indexOf("-") != -1) {
                var str = hukeID;
                var patt1 = /\d.*\-(\d.*)/;
                var namey = str.match(patt1)
                var hukeID = namey[1];
            }
            parseVideo(hukeID)
        });
    });



    function broadcastVideo(m3u8url) {

        const dp = new DPlayer({
            container: document.getElementById('dplayer'),
            autoplay: false,
            loop: true,
            screenshot: true,
            video: {
                url: m3u8url,
                type: 'auto',
            },
        });

        dp.play()

        dp.on('playing', function() {
            //回调播放成功!! 可以向后台推送此次播放已成功
            videoParse = true;
            dialog.msg('课程播放成功！右侧可下载视频源文件和复制视频下载地址')
        })
    }
    function parseVideo(hukeID) {

        // return dialog.msg("解析视频地址正确")

        $('.parse-btn').addClass('disabled').html('获取中...');

        var parse_url = '{:url('index/index/parse')}'

        $.ajax({
            url: parse_url,
            data: {id: hukeID, link:link},
            dataType: 'json',
            type: 'POST',
            timeout: 120000,
            success: function(result) {
            if (result.code <= 0) {
                return dialog.msg(result);
            } else if (result.code == 200) {
                source_url = result.source_url;
                m3u8_url = result.m3u8_url;
                media_url = result.media_url
                $(".Clipboard").attr('data-clipboard-text', m3u8_url)
                broadcastVideo(m3u8_url);
            }
        },
        complete: function(request, status) {
            $('.parse-btn').removeClass('disabled').html('获取资源');
            if (status == 'error') {
                dialog.msg('页面错误，请联系管理员！');
            } else if (status == 'timeout') {
                dialog.msg('数据提交超时，请重试！');
            }
        }
    })
    }
    $("#download-source").click(function() {

        if (!videoParse){
            return  dialog.msg('请先解析视频再下载源文件')
        }

        if (source_url == '') {
            return dialog.msg('本节课无源文件')
        }

        window.location = source_url
    });
    $("#download-media").click(function() {
        if (!videoParse){
            return  dialog.msg('请先解析视频再下载素材文件')
        }

        if (media_url == ''){
            return dialog.msg('本节课无素材文件')
        }

        window.location = media_url
    })
    $("#copy_link").click(function () {



        if (!videoParse){
            return  dialog.msg('请先解析视频再下载素材文件')
        }
        
        setTimeout(function () {
            return dialog.msg('可以点击右部菜单查看视频下载教程')
        }, 3000)
    })


</script>
